//
//  TableViewController.m
//  02-动态拦截系统原声API
//
//  Created by ItHeiMa on 2017/9/2.
//  Copyright © 2017年 itHeima. All rights reserved.
//

#import "TableViewController.h"

#import "NSObject+RunTime.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.tableView.sectionIndexColor = [UIColor redColor];
    
    NSLog(@"%@",self.tableView.subviews);
   
    id indexView = self.tableView.subviews.lastObject;
    NSLog(@"%@",[indexView getMethodlist]);
    
    UIFont *font = [UIFont fontWithName:@"Cochin" size:20];
    
   
    CGRect rect = CGRectMake(self.view.bounds.size.width-20, 0, 20, self.view.bounds.size.height);
    [self.tableView.subviews.lastObject setValue:[NSValue valueWithCGRect:rect] forKey:@"frame"];
    
    //开始动态拦截系统原声的API的底层
    
    //1.获取方法签名
    SEL sel = NSSelectorFromString(@"setFont:");
    
    NSMethodSignature *signature = [self.tableView.subviews.lastObject methodSignatureForSelector:sel];
    
    //2.获取方法实现,动态拦截参数
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    //2.1 设置方法的执行者
    [invocation setTarget:self.tableView.subviews.lastObject];
    //2.2设置执行的方法名
    [invocation setSelector:sel];
    //2.3设置执行的参数
    [invocation setArgument:&font atIndex:2];
    //2.4 开始拦截
    [invocation invoke];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSArray<NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//    return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G"];
    
    //所有的对象无论是图片还是字符串底层都是二进制,唯一不同的是 字符串使用的UTF8编码  emoji表情使用的是unicode编码
    //1.第一种方式
    //return @[@"🔍",@"⭐️",@"😍",@"😘"];
    //2.使用unicode编码16进制来实现
    //return @[[NSString stringWithFormat:@"%C",0x26C4]];
    //3.使用二进制表示字符串
    Byte b[2];
    b[0] = 0xff & 38;
    b[1] = 0xff & 20;
    NSData *data = [NSData dataWithBytes:b length:2];
    
    
    Byte c[4];
    c[0] = 0xff & 0;
    c[1] = 0xff & 1;
    c[2] = 0xff & 243;
    c[3] = 0xff & 10;
    NSData *data1 = [NSData dataWithBytes:c length:4];
    return @[[[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding],[[NSString alloc] initWithData:data1 encoding:NSUTF32StringEncoding]];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
